package com.jackiechancinema.ssm.service.Impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jackiechancinema.ssm.bo.ContractBo;
import com.jackiechancinema.ssm.bo.DailyBo;
import com.jackiechancinema.ssm.config.SystemContant;
import com.jackiechancinema.ssm.dao.ContractDao;
import com.jackiechancinema.ssm.dao.DailyDao;
import com.jackiechancinema.ssm.dto.ContractExcelDto;
import com.jackiechancinema.ssm.dto.DailyExcelDto;
import com.jackiechancinema.ssm.entity.Contract;
import com.jackiechancinema.ssm.entity.Daily;
import com.jackiechancinema.ssm.listener.ContractReadListener;
import com.jackiechancinema.ssm.listener.DailyReadListener;
import com.jackiechancinema.ssm.service.Inte.ContractService;
import com.jackiechancinema.ssm.service.Inte.DailyService;
import com.jackiechancinema.ssm.utils.BeanUtil;
import com.jackiechancinema.ssm.vo.PageVo;
import com.jackiechancinema.ssm.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class DailyServiceImpl implements DailyService {
    @Autowired
    private DailyDao dailyDao;
    @Autowired
    private DailyReadListener listener;

    //新增
    @Override
    public R insert(Daily daily) {
        if (dailyDao.insert(daily)>0){
            return R.ok(null);
        }else{
            return R.fail();
        }

    }
    //查询
    @Override
    public PageVo<Daily> queryPage(DailyBo dailyBo) {
        if (dailyBo != null){
            Daily daily = BeanUtil.copyProperty(Daily.class, dailyBo, dailyBo.getClass().getDeclaredFields());
            PageHelper.startPage(dailyBo.getPage(), dailyBo.getLimit());//开启分页
            PageInfo<Daily> pageInfo = new PageInfo<Daily>(dailyDao.selectAll(daily));
            return new PageVo<Daily>(SystemContant.PAGE_R,"OK",pageInfo.getTotal(),pageInfo.getList());
        }
        return new PageVo<Daily>(SystemContant.PAGE_R,"OK",0,null);
    }
    //删除
    @Override
    public R deleteById(int id) {
        if (dailyDao.delete(id)>0){
            return R.ok(null);
        }else{
            return R.fail();
        }
    }
    //批量删除
    @Override
    public R deleteBatch(List<Daily> dailys) {

        List<Integer> list = new ArrayList<>();
        for (Daily daily : dailys) {
            list.add(daily.getId());
        }

        if (dailyDao.batchDel(list)>0){
            return R.ok(null);
        }else{
            return R.fail();
        }
    }
    @Override
    public R update(Daily daily) {
        if (dailyDao.update(daily)>0){
            return R.ok(null);
        }else{
            return R.fail();
        }
    }

    @Override
    public R insertBatch(MultipartFile file) {
        if (!file.isEmpty()){
            try {
                EasyExcel.read(file.getInputStream(), DailyExcelDto.class,listener).sheet().doRead();
                return R.ok("OK");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return R.fail();
    }

    @Override
    public void exportExcel(DailyBo bo, HttpServletResponse response) {
        //1.设置响应消息头，目的：告诉请求方，这个是下载
        String fn=System.currentTimeMillis()+".xlsx";
        //设置响应头  告知浏览器，要以附件的形式保存内容   filename=浏览器显示的下载文件名
        response.setHeader("content-disposition","attachment;filename="+fn);

        //2.组装查询条件
        Daily daily=new Daily();
        if(StringUtils.hasLength(bo.getName())){
            daily.setName(bo.getName());
        }
        if(StringUtils.hasLength(bo.getMonth())){
            daily.setMonth(bo.getMonth());
        }
        if(StringUtils.hasLength(bo.getContent())){
            daily.setContent(bo.getContent());
        }
        if(StringUtils.hasLength(bo.getComplete())){
            daily.setComplete(bo.getComplete());
        }
        if(bo.getDay() != null){
            daily.setDay(bo.getDay());
        }
        //3.获取要下载的数据
        List<Daily> list=dailyDao.selectAll(daily);
        try {
            //4.生成Excel，并将数据写出到响应流中
            EasyExcel.write(response.getOutputStream(),Daily.class).sheet().doWrite(list);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
